/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatsoft.client.ui;

import ChatSoft.model.ServerInfo;
import chatsoft.client.buz.ClientController;
import chatsoft.client.voicechat.Tx;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;

/**
 *
 * @author KID
 */
public class ChatSoft_Client {

    public ObjectOutputStream outStream;
    public ObjectInputStream inStream;
    public Socket socket;
    public Boolean serverAvail;
    
    public ChatSoft_Client(String ip, int port) {
        try {
            socket = new Socket(ip, port);
            outStream = new ObjectOutputStream(socket.getOutputStream());
            inStream = new ObjectInputStream(socket.getInputStream());
            serverAvail = true;
        } 
        catch (UnknownHostException ex) {
            JOptionPane.showMessageDialog(null, "Server not availble...!");
            serverAvail = false;
        } 
        catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Server not availble...!");
            serverAvail = false;
        }
    }
    public static void main(String[] args) {
        
        // Convert params to a IP address if any.
        if (args.length > 0) {
            ServerInfo.ip = args[0];
            ServerInfo.port = Integer.parseInt(args[1]);
        }
        
        System.out.println("Connecting to server at " + ServerInfo.ip + ":" + ServerInfo.port);
        System.out.println("...");
        ChatSoft_Client cc = new ChatSoft_Client(ServerInfo.ip, ServerInfo.port);
        
        if (cc.serverAvail) {//neu sever da mo, tao ra 1 frame login...!
            LoginFrame loginFrame = new LoginFrame(cc.outStream);
            loginFrame.setVisible(true);
            ClientController controller = new ClientController(cc.inStream, cc.outStream, loginFrame);
            controller.start();
        }
        
    }
}
